﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_roleMenuManager : BaseListPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        PageSize = 10;
        TableName = "RoleMenu"; Pk = "Id";
        if (!this.IsPostBack)
        {
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);

            BindDDL();
        }
    }

    public void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<Role>(ref rolelist,
            RoleBLL.GetInstance().GetList(), "RoleName", "RoleId", "角色");
        UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref menulist,
            MenuBLL.GetInstance().GetList(), "MenuName", "MenuId", "角色");
    }

    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        PageIndex = Pager.CurrentPageIndex - 1;
        BindData(ref rptList);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("roleMenuAdd.aspx");
    }

    public string getRoleName(object o)
    {
        string result = "未知";
        int id = Convert.ToInt32(o);
        Role role = RoleBLL.GetInstance().GetEntity(r => r.RoleId == id);
        if(role!=null)
        {
            result = role.RoleName;
        }
        return result;
    }

    public string getRoleStatus(object o)
    {
        string result = "未知";
        int id = Convert.ToInt32(o);
        Role role = RoleBLL.GetInstance().GetEntity(r => r.RoleId == id);
        if (role != null)
        {
            result = role.IsUsing == 1 ? "已启动" : "已禁用";

        }
        return result;
    }

    public string getMenuName(object o)
    {
        string result = "未知";
        int id = Convert.ToInt32(o);
        OneShoppingModel.Menu menu = MenuBLL.GetInstance().GetEntity(r => r.MenuId == id);
        if(menu!=null)
        {
            result = menu.MenuName;
        }
        return result;
    }

    public string getMenuParentName(object o)
    {
        string result = "未知";
        int id = Convert.ToInt32(o);
        OneShoppingModel.Menu menu = MenuBLL.GetInstance().GetEntity(r => r.MenuId == id);
        if (menu.ParentMenu == 0)
        {
            result = "一级菜单";
        }
        else
        {
            OneShoppingModel.Menu m = MenuBLL.GetInstance().GetEntity(r => r.MenuId == menu.ParentMenu);
            if(m!=null)
            {
                result = m.MenuName;
            }
        }
        return result;
    }

    public string operation(object o)
    {
        string result = "";
        int id = Convert.ToInt32(o);
        RoleMenu rolemenu = RoleMenuBLL.GetInstance().GetEntity(r => r.Id == id);
        OneShoppingModel.Menu menu = MenuBLL.GetInstance().GetEntity(r => r.MenuId == rolemenu.MenuId);
        if (menu.ParentMenu == 0)
        {
            result = "<font color='red'>一级菜单无法修改</font>";
        }
        else
        {
            result = "<a href='roleMenuAlter.aspx?id=" + id + "'>修改信息</a>";
        }
        return result;
    }

    public string getMenuStatus(object o)
    {
        string result = "未知";
        int id = Convert.ToInt32(o);
        OneShoppingModel.Menu menu = MenuBLL.GetInstance().GetEntity(r => r.MenuId == id);
        if (menu != null)
        {
            result = menu.Status == 1 ? "已启用" : "已禁用";
        }
        return result;
    }


    protected void btndelete_Click(object sender, EventArgs e)
    {
        string[] id = SelectId();
        if (id != null)
        {
            foreach (var i in id)
            {
                int rmid = Convert.ToInt32(i);
                bool rrm= RoleMenuBLL.GetInstance().Delete(rmid);
                if(!rrm)
                {
                    UtilityMessageBox.Show(this, i+"删除失败");
                }
            }
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
    }
    private string[] SelectId()
    {
        string[] id = null;
        string ids = "";
        try
        {
            ids = Request["ids"];
            if (ids.Length == 0)
            {
                UtilityMessageBox.Show(this, "未选择");
                return null;
            }
        }
        catch  
        {
            UtilityMessageBox.Show(this, "未选择");
            return null;
        }
        id = ids.Split(',');
        return id;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //---------------------根据角色查询...
        Condition = "";
        string id = this.rolelist.SelectedValue;
        if (!id.Equals("-1"))
        {
            Condition = Condition + " and RoleId = " + id;
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }
    protected void btnSearch_menu_Click(object sender, EventArgs e)
    {
        //---------------------根据菜单查询...
        Condition = "";
        string id = this.menulist.SelectedValue;
        if (!id.Equals("-1"))
        {
            Condition = Condition + " and MenuId = " + id;
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }
}